//
//  HomeViewCell.swift
//  JIMWeiBo
//
//  Created by 陈恒均 on 2018/2/12.
//  Copyright © 2018年 陈恒均. All rights reserved.
//

import UIKit
import SDWebImage

private let Margin : CGFloat = 15
private let itemMargin : CGFloat = 10

class HomeViewCell: UITableViewCell {
    // MARK:- 控件属性
    @IBOutlet weak var iconView: UIImageView!
    
    @IBOutlet weak var verifiedView: UIImageView!
    
    @IBOutlet weak var screenNameLabel: UILabel!
    
    @IBOutlet weak var vipView: UIImageView!
    
    @IBOutlet weak var timeLabel: UILabel!

    @IBOutlet weak var sourceLabel: UILabel!
    
    @IBOutlet weak var contentLabel: UILabel!
    
    @IBOutlet weak var picView: PicCollectionView!
    
    @IBOutlet weak var retweetedContentLabel: UILabel!
    
    @IBOutlet weak var retweetedBgView: UIView!
    
    @IBOutlet weak var bottomToolView: UIView!
    // MARK:- 约束属性
    @IBOutlet weak var contentLabelWCont: NSLayoutConstraint!
    
    @IBOutlet weak var collectionViewWcons: NSLayoutConstraint!
    @IBOutlet weak var collectionViewHcons: NSLayoutConstraint!
    @IBOutlet weak var retweetedContentLabelTopCons: NSLayoutConstraint!
    
    @IBOutlet weak var picViewBottomCons: NSLayoutConstraint!
  
    // MARK:- 模型属性
    var viewModel : StatusViewModel?{
        didSet{
            guard let viewModel = viewModel else {
                return
            }
            
            iconView.sd_setImage(with: viewModel.profileURL as URL? , placeholderImage: UIImage(named: "avatar_default_small"), options: [], completed: nil)
            
            verifiedView.image = viewModel.verifiedImage
            
            verifiedView.image = viewModel.verifiedImage
            
            screenNameLabel.text = viewModel.status?.user?.screen_name
            
            vipView.image = viewModel.vipImage
            
            timeLabel.text = viewModel.createAtText
            
            sourceLabel.text = viewModel.sourceText
            
            contentLabel.attributedText = FindEmotion.shareInstance.findAttrString(statusText: viewModel.status?.text, font:contentLabel.font)
            
            screenNameLabel.textColor = viewModel.vipImage == nil ? UIColor.black : UIColor.orange
            
            
            
            //计算collectionView的宽度和高度的约束
            let picViewSize = calulatePicViewSize(count: viewModel.picURLs.count)
            
            collectionViewWcons.constant = picViewSize.width
            collectionViewHcons.constant = picViewSize.height
            
            picView.picURLS = viewModel.picURLs
            
            //设置转达微博的正文
            if viewModel.status?.retweeted_status != nil {
                if let screenName = viewModel.status?.retweeted_status?.user?.screen_name , let retweedteText = viewModel.status?.retweeted_status?.text{
                    let retweedtext = "@ " + "\(screenName) :" + retweedteText
                    retweetedContentLabel.attributedText = FindEmotion.shareInstance.findAttrString(statusText: retweedtext, font: retweetedContentLabel.font)
                    
                }
                retweetedBgView.isHidden = false
                retweetedContentLabelTopCons.constant = 15
                
            } else{
                retweetedContentLabel.text = nil
                retweetedBgView.isHidden = true
                retweetedContentLabelTopCons.constant = 0
            }
            
            if viewModel.cellHeight == 0 {
                layoutIfNeeded()
                viewModel.cellHeight = bottomToolView.frame.maxY
                
            }
            
        }
    }
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        contentLabelWCont.constant = UIScreen.main.bounds.width - 2 * Margin
        /*
        let layout = picView.collectionViewLayout as! UICollectionViewFlowLayout
        let imageViewWH = (UIScreen.main.bounds.size.width - 2 * Margin - 2 * itemMargin) / 3
        layout.itemSize = CGSize(width: imageViewWH, height: imageViewWH)
        */
        
        
        
    }

}
// MARK:- 计算collectionView的宽度和高度的约束
extension HomeViewCell{
    func calulatePicViewSize(count : Int) -> CGSize {
        if count == 0 {
//            picViewBottomCons.constant = 0
            return CGSize.zero
            
        }
//        picViewBottomCons.constant = 10
        let layout = picView.collectionViewLayout as! UICollectionViewFlowLayout
        
        //单张配图
//        if count == 1 {
//            let urlString = viewModel?.picURLs.last?.absoluteString
//            let urlstr = (urlString! as NSString).components(separatedBy: "/")
//            print(urlstr)
//            let urlstr2 = urlstr.last
//
//            let image  =  SDWebImageManager.shared().imageCache?.imageFromCache(forKey: urlstr2)
//            let imageW = image?.size.width ?? 0
//            let imageH = image?.size.height ?? 0
//
//
//            layout.itemSize = CGSize(width:imageW * 2, height:imageH * 2)
//
//
//            return CGSize(width: imageW * 2, height:imageH * 2)
//
//
//        }
        
        
        //计算collectionView中itemSize
        let imageViewWH = (UIScreen.main.bounds.width - 2 * Margin - 2 * itemMargin) / 3
 
        //计算其他张图片时layout的itemSize
        layout.itemSize = CGSize(width: imageViewWH, height: imageViewWH)
        
        if count == 4 {
            let picViewWH = imageViewWH * 2 + itemMargin
            return CGSize(width: picViewWH, height: picViewWH)
            
        }
        
        //计算其他长配图
        let rows = CGFloat((count - 1) / 3 + 1)
        
        let picViewH = rows * imageViewWH + (rows - 1) * itemMargin
        
        let picViewW = UIScreen.main.bounds.width - (2 * Margin)
        
        return CGSize(width: picViewW, height: picViewH)
        
        
        
        
    }
}
